from pydantic import BaseModel
from typing import Generic, TypeVar, Optional, List

T = TypeVar('T')


class TableResponse(BaseModel, Generic[T]):  # 继承 Generic[T]
    code: int = 200
    message: str = "success"
    data: Optional[T] = None  # 明确使用泛型
    totalCount: int = 0
    pageIndex: int = 0
    pageSize: int = 0

    @classmethod
    def success(cls, data: T = None, message: str = "success",
                totalCount: int = 0, pageIndex: int = 0, pageSize: int = 0) -> 'TableResponse[T]':  # 返回类型标注
        return cls(
            code=200,
            message=message,
            data=data,
            totalCount=totalCount,
            pageIndex=pageIndex,
            pageSize=pageSize
        )
